package com.uchengwang.ui.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtil {
	public static final String DATE_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMAT_YMD = "yyyy-MM-dd";
	public static final String DATE_FORMAT_YMD2 = "yyyy.MM.dd";
	public static final String DATE_FORMAT_YM = "yyyy-MM";
	public static final String DATE_FORMAT_MD = "MM-dd";
	public static final String DATE_FORMAT_MS = "mm:ss";
	public static final String DATE_FORMAT_HM = "HH:mm";
	public static final String DATE_FORMAT_MDHM = "MM-dd HH:mm";
	public static final String DATE_FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
	public static final String DATE_FORMAT_YMD_Z = "yyyy年MM月dd日";
	public static final String DATE_FORMAT_MDHM_Z = "MM月dd日 HH:mm";
	public static final String DATE_FORMAT_MDHM_Z_D = "MM月dd日";
	public static final String DATE_FORMAT_MMSS = "mmss";
	public static final String DATE_FORMAT_YMDHMS_2 = "yyyyMMddHHmmss";
	public static final String DATE_FORMAT_M_D = "MM/dd";

	private static DateUtil dateUtil;

	private DateUtil(){}

	public static DateUtil getDateUtil() {
		if (dateUtil == null){
			dateUtil = new DateUtil();
		}
		return dateUtil;
	}

	public String getDateByMillis(String format, long millis){
		return new SimpleDateFormat(format).format(new Date(millis));
	}

	public long getMillisByDate(String formatString, String birthdayString){
		SimpleDateFormat format = new SimpleDateFormat(formatString);
        Date date = null;
        Calendar cal = Calendar.getInstance();
		try {
			date = format.parse(birthdayString);
			cal.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
       
        return cal.getTimeInMillis();
	}


	public String getDateByDate(String formatString, Date date){
		SimpleDateFormat format = new SimpleDateFormat(formatString);
		return format.format(date);
	}

	public long getLongByDate(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal.getTimeInMillis();
	}
	
	//判断字符串是不是数字
	public boolean isNumeric(String str){
	   Pattern pattern = Pattern.compile("[0-9]*");
	   Matcher isNum = pattern.matcher(str);
	   if( !isNum.matches() ){
	       return false; 
	   } 
	   return true; 
	}
	
//	//判断字符串是否是字母和数字的组合
//	public static boolean isPassword(String str){
//		Pattern pattern = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{1,20}$");  
//		Matcher ispwd = pattern.matcher(str);
//		if(!ispwd.matches()){
//			return false;
//		}
//		return true;
//	}
	
	public String getTimeString(long time){
		int ret = 0;
		try {
			ret = isYeaterday(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		if(ret==0){
			return "昨天";
		}else if(ret==1){//前天以前
			return dateUtil.getDateByMillis(DateUtil.DATE_FORMAT_MDHM_Z_D, time);
		}else{//今天
			return dateUtil.getDateByMillis(DateUtil.DATE_FORMAT_HM, time);
		}
	}

	/**
	 * 类似微信显示时间
	 * */
	public String getTimeDateWx(long time){
		int ret = 0;
		try {
			ret = isYeaterday(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		if(ret==0){
			return "昨天 "+dateUtil.getDateByMillis(DateUtil.DATE_FORMAT_HM, time);
		}else if(ret==1){//前天以前
			return dateUtil.getDateByMillis(DateUtil.DATE_FORMAT_MDHM, time);
		}else{//今天
			return dateUtil.getDateByMillis(DateUtil.DATE_FORMAT_HM, time);
		}
	}

	public String getTimeStringDate(long time){
		int ret = 0;
		try {
			ret = isYeaterday(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		if(ret==0){
			return "昨天 "+dateUtil.getDateByMillis(DateUtil.DATE_FORMAT_HM, time);
		}else if(ret==1){//前天以前
			return dateUtil.getDateByMillis(DateUtil.DATE_FORMAT_MDHM, time);
		}else if (ret ==2){//前天
			return "前天 "+dateUtil.getDateByMillis(DateUtil.DATE_FORMAT_HM, time);
		}else{//今天
			long currTime = System.currentTimeMillis();
			long result = currTime - time;
			long nh = 1000 * 60 * 60;//一小时的毫秒数
			long nm = 1000 * 60;//一分钟的毫秒数
			long ns = 1000;//一秒钟的毫秒数
			long hour = result / nh;//计算差多少小时
			long min = result % nh / nm;//计算差多少分钟
			long sec = result % nh % nm / ns;//计算差多少秒

			if (hour != 0) {
				return hour + "小时前";
			}
			if (min != 0) {
				return min + "分钟前";
			}
			return "刚刚";
		}
	}
	
	private int isYeaterday(long time) throws ParseException {
		Date newTime =new Date();
        //将下面的 理解成  yyyy-MM-dd 00：00：00 更好理解点
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	    String todayStr = format.format(newTime);
	    Date today = format.parse(todayStr);
	    //昨天 86400000=24*60*60*1000 一天
	    if((today.getTime()-time)>0 && (today.getTime()-time)<=86400000) {
            return 0;//昨天
        }
	    else if((today.getTime()-time)<=0){ //至少是今天
	    	return -1;
	    }
		else if ((today.getTime()-time)>0 && (today.getTime()-time)<=172800000){
			return 2;//前天
		}
	    else{ //前天以前
	    	return 1;
	    }
	}

	/**
	 * 根据时间毫秒值  返回分:秒 格式
	 * */
	public String getTimeMS(long time){
		if (time >0){
			int t= (int) (time/1000);
			int minute =  t/60;//分钟
			int second = t % 60;

			if (second >=10){
				return minute + ":"+second;
			}
			return minute + ":0"+second;
		}
		return "";
	}
}
